<%@ page pageEncoding="UTF-8"%>
<%@page import="itsm.isperp.module.entity.app.AppRole"%>
<%@page import="java.util.List"%>
<%@page import="itsm.isperp.framework.core.context.ContextHolder"%>
<%@page import="itsm.isperp.module.service.app.AppRoleService"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="isperp" uri="/WEB-INF/tlds/isperp.tld"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<style type="text/css">
.ui-jqgrid-resize-mark {
	display: none
}

.ui-jqgrid-bdiv tr.ui-row-ltr {
	height: 30px;
}
</style>
<script type="text/javascript">
	function userTree_onNodeClick(event, treeId, treeNode) {
		userListSelectorGrid.resetSearchFilters();
		userListSelectorGrid.search({
			"field" : "department_treenode_path",
			"op" : "bw",
			"data" : treeNode.treenodePath
		});
	}
</script>
<table class="" style="width:96%;padding: 0;margin:2%">
	<c:choose>
		<c:when test="${processMode eq 2 }">
			<tr>
				<td style="padding:8px">
					<div class="row">
						<p class="_100 small">
							<label for="" style="font-weight: bold;vertical-align: middle;">下一步处理人</label>
							<select id="username" name="username" class="${searchCls }">
								<c:forEach items="${users }" var="u">
									<option value="${u.username }">${u.fullname }</option>
								</c:forEach>
							</select>
						</p>
					</div></td>
			</tr>
			<c:set var="suggestionRequired" value="textarea"></c:set>
		</c:when>
		<c:when test="${'0' eq processMode  and '1' eq commitType }">
			<tr>
				<td style="padding:8px">
					<div class="row">
						<p class="_100 small">
							<label for="" style="font-weight: bold;vertical-align: middle;">下一步处理人</label>
							<select id="username" name="username" class="${searchCls }">
								<c:forEach items="${users }" var="u">
									<option value="${u.username }">${u.fullname }</option>
								</c:forEach>
							</select>
						</p>
					</div></td>
			</tr>
			<c:set var="suggestionRequired" value="textarea"></c:set>
		</c:when>


		<c:when test="${'0' eq processMode  and '2' eq commitType }">
			<tr>
				<td style="padding:8px">
					<%-- <div class="row">
						<p class="_100 small">
							<label for="" style="font-weight: bold;vertical-align: middle;">下一步处理人</label>
							<br>
							<c:forEach items="${users }" var="u" varStatus="st">
								<input type="checkbox" id="u_${st.index }" name="username"
									value="${u.username }" checked="checked">
								<label for="u_${st.index }">${u.fullname }</label>
							</c:forEach>

						</p>
					</div> --%>
					<div class="department-user-table" style="border:node;height:100%">
						<div
							style="width:159px;vertical-align: top;float:left;border-right:1px solid #eee">

							<div id="accordion">
								<h3>按部门</h3>
								<div>
									<isperp:tree id="userTree"
										cssStyle="height:300px;overflow:auto">
										<isperp:async enable="true" autoParam="'id'"
											url="app/department/getTree?parentId=">
										</isperp:async>
										<isperp:callback onClick="userTree_onNodeClick">
										</isperp:callback>
									</isperp:tree>
								</div>
								<h3>按角色</h3>
								<div style="height:300px;overflow:auto">
									<%
										AppRoleService appRoleService = ContextHolder.getSpringBean("appRoleService");

												List<AppRole> roles = appRoleService.findAll();

												for (AppRole role : roles) {
									%>
									<p class="nui-txt-link" data-id="<%=role.getId()%>"><%=role.getName()%></p>

									<%
										}
									%>

								</div>
							</div>
						</div>
						<div style="vertical-align: top;width:306px;float:left">
							<isperp:grid
								url="app/user/deptmentSysUser?rolenames=&ifIncludeNoRoleUser=}&projectIdForPrjMember="
								autoload="false" filterToolbar="true" showSearchbar="true"
								showToolbar="false" multikey="true" rowNum="10"
								rownumbers="false" idFieldName="name"
								gridId="userListSelectorGrid" columnFilter="false" height="300"
								pager="true" cusFormatterActions="false"
								onSelectRow="onSelectRowEvent(rowid,status);"
								afterGridCreate="userListSelectorGrid.search()"
								ondbClickRowEvent="ondbClickRowEventEvent();">
								<jsp:attribute name="searchbar">
				<table class="search-table" style="display: none">
					<tbody>
						<tr>
							<td>
							    <!-- <input type="hidden" field="name" op="isnotnull" value="1"> -->
							</td>
						</tr>
					</tbody>
				</table>
			
			</jsp:attribute>
								<jsp:body>	
			<isperp:column name="id" title="id" hidden="true" width="10"></isperp:column>
			<isperp:column name="name" title="帐号" width="110">
			</isperp:column>
			<isperp:column name="fullname" title="姓名" width="100">
			</isperp:column>
			<isperp:column name="position" title="职位" width="90">
			</isperp:column>
			<isperp:column name="app_department_id" hidden="true"
										title="appDepartmentId">
			</isperp:column>
			<isperp:column name="department_treenode_path" hidden="true"
										title="departmentTreenodeNamePath">
			</isperp:column>
			</jsp:body>
							</isperp:grid>

						</div>

						<div
							style="vertical-align:top;float:left;width:65px;overflow: hidden">
							<div style="padding:10px 0px 0px 5px" id="selectedRows"></div>
						</div>
					</div></td>
			</tr>
			<c:set var="suggestionRequired" value="textarea"></c:set>
		</c:when>

		<c:otherwise>
			<c:set var="suggestionRequired" value="required"></c:set>
		</c:otherwise>
	</c:choose>

	<tr>
		<td style="padding:8px">
			<div class="row no-bg">
				<p class="_100">
					<label for="" style="font-weight: bold;">处理意见</label>
					<textarea id="suggestion" name="suggestion" rows="6"
						style="width:96%" class="${suggestionRequired }"></textarea>
				</p>
			</div></td>
	</tr>
	<tr>
		<td style="padding:8px"><div class="row">
				<p class="_100 small">
					<label for="" style="font-weight: bold;">常用意见</label> <select
						data-width="100%" onchange="appendToSuggestion(this)">
						<option value="" selected="selected"></option>
						<c:forEach items="${views }" var="item">
							<option value="${item.name }">${item.name }</option>
						</c:forEach>
					</select>
				</p>
			</div></td>
	</tr>

</table>

<isperp:buttonList buttonListId="approvelBtns">
	<button class="submit" onclick="submitProcess()">
		<span class="icon icon-ok"></span>提交
	</button>
</isperp:buttonList>
<script type="text/javascript">
	function appendToSuggestion(obj) {
		var sug = $("#suggestion");
		sug.val(sug.val() + $(obj).val());
	}

	function submitProcess() {

		var $sug = $("#suggestion").val();
		/* if (($sug == "" || $sug == undefined)) {
			isperp.alert("请填写处理意见");
			return false;
		} */

		var processMode = "${processMode}";
		var commitType = "${commitType}";
		if (processMode == "0" && commitType != "0") {
			var uname = "";

			if (commitType == "1") {
				uname = $("#username").val();
			} else if (commitType == "2" || commitType == "3") {
				$("input[name=username]:checked").each(function(index, item) {
					if (index != 0) {
						uname += ",";
					}
					uname += $(item).val();
				});
			}

			if (uname == "") {
				isperp.errorAlert("请选择处理人");
				return;
			}
		}

		$("#approvelBtns").hide();
		$("#approvelBtns").parent().append("<label style='float:right;font-size:14px;color:red;'>正在提交，请稍后......</lable>");

		$.post("workflow/processRun/gotoNextStep", {
			"key" : "${processKey}",
			"nextTaskId" : "${nextTaskId}",
			"nextTransitionId" : "${nextTransitionId}",
			"processId" : "${processId}",
			"name" : "${name}",
			"businessKey" : "${businessKey}",
			"username" : uname,
			"processMode" : "${processMode}",
			"commitType" : "${commitType}",
			"fromId" : "${fromId}",
			"fromProcessKey" : "${fromProcessKey}",
			"suggestion" : $sug
		}, function(result) {
			if (result.success) {
				isperp.alert(result.data);
				isperp.reloadParentControlById();
				isperp.closeDialog({
					closeParent : true
				});

			}

		});

	}
</script>
<script type="text/javascript">
    $("#accordion" ).accordion({autoHeight: false,heightStyle:"content"}); 
    
    $(".nui-txt-link").click(function(){
    	 $(".nui-txt-link").removeClass("active");
    	 $(this).addClass("active");
    	 userListSelectorGrid.resetSearchFilters();
    	 userListSelectorGrid.search({
    			"field" : "name",
    			"op" : "in",
    			"data" : " (SELECT app_user_name FROM app_user_role WHERE app_role_id ='"+$(this).data("id")+"')"
    		});
    });
    
    
    
	function onSelectRowEvent(rowid,status){
		if(!status){
			$("#selectedRows").find("#d_"+rowid).remove();
			return;
		}
		var rowData = userListSelectorGrid.getRowData(rowid);
		var value=rowData.name;
		var find=false;
		$("#selectedRows .box").each(function(index,item){
			if (value==$(this).data("value")){
				find=true;
				return;
			}
		});
		if(find){
			return false;
		}
		var html='<div class="box" style="padding:4px;margin-top:3px" data-value="'+
			value + '" data-name="'+rowData.fullname+'" id="d_'+rowData.id+'">';
		html+='<a href="javascript:void(0);" onclick="deleteSelected(\''
				+rowData.id+'\')"><span class="icon icon-remove "></span>'+rowData.fullname+'</div>';
		if(1==1){
			$("#selectedRows").html(html);
		}else{
			$("#selectedRows").append(html);
		}
	}
	
	
	function ondbClickRowEventEvent(){
		if(1==1){
			var rowData = userListSelectorGrid.getRowData();
			var value=rowData.name;
			window.parent.userSetSelected([value,rowData.fullname,rowData]);
		}
	}
	
	function deleteSelected(id){
		userListSelectorGrid.jqGrid.setSelection(id, false);
		$("#selectedRows").find("#d_"+id).remove();
	}
	
	function getSelectedValue(){ 
		var values,names;
		$("#selectedRows .box").each(function(index,item){
			if(index==0){
				values=$(this).data("value");
				names=$(this).data("name");
			}else{
				values+=","+$(this).data("value");
				names+=","+$(this).data("name");
			}
		});
		return [values,names];
	}
	
	function getSelectedRowValue(){ 
		var values,names;
		$("#selectedRows .box").each(function(index,item){
			if(index==0){
				values=$(this).data("value");
				names=$(this).data("name");
			}else{
				values+=","+$(this).data("value");
				names+=","+$(this).data("name");
			}
		});
		return [values,names,userListSelectorGrid.getRowData()];
	}
	
	</script>

